//
//  CSSettleDeliveryUpPlateVC.m
//  ProjectforHDX
//
//  Created by yinda on 16/8/10.
//  Copyright © 2016年 H.DX. All rights reserved.
//

#import "CSSettleDeliveryUpPlateVC.h"
#import "settleDeliveryUpCell.h"
#import "SettleDeliverPlate.h"

@interface CSSettleDeliveryUpPlateVC ()<UITableViewDataSource,UITableViewDelegate,settleDeliveryUpcellDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)UIView *bottomView;

@property(nonatomic ,strong)SettleDeliverPlate *confirmPlate;

@property (nonatomic,assign)CGFloat cellHeight;

@property (nonatomic, strong) UIButton *confirmBtn;

@property(nonatomic, assign)NSInteger tag;

@property (nonatomic, strong) settleDeliveryUpCell *deliveryCell;

@property (nonatomic, strong) RZCustomButtom *clickBtn;

@property (nonatomic, strong) UIImageView *nodata;

@end

@implementation CSSettleDeliveryUpPlateVC





-(NSMutableArray *)plateArr
{
    if (!_plateArr) {
        _plateArr= [[NSMutableArray alloc]init];
    }
    return _plateArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setupView];
    [self requestPlates];

}



#define CELL_LINE_COLOR [UtilCheck hexStringToColor:@"e1e1df" alpha:1]
#pragma -mark 初始化窗口
-(void)setupView
{
    self.navColor = SYSTEM_COLOR;
    self.labnav.textColor = [UIColor whiteColor];
    self.labnav.text = [UtilCheck language:@"local.updatePlate"];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-60-60) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource =self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];
    
//    UIImageView *nodataImage = [[UIImageView alloc]init];
//    //        nodataImage.center = self.tabelView.center;
//    nodataImage.image = BDFILEIMAGENAEM(@"default/none.png");
//    [nodataImage setSize:CGSizeMake(100, 130)];
//    nodataImage.top = 150;
//    nodataImage.left = [UIScreen mainScreen].bounds.size.width * 0.5 - nodataImage.bounds.size.width * 0.5;
//    self.nodata = nodataImage;
//    [self.view addSubview:nodataImage];
    
    //底部view
    CGFloat bottomVH = 60;
    UIView *bottomV = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT-bottomVH-60, SCREEN_WIDTH, 60)];
    [bottomV addSoldLine:1.0 soldcolor:CELL_LINE_COLOR pointA:CGPointMake(0, 0) pointB:CGPointMake(SCREEN_WIDTH, 0)];
    bottomV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomV];
    self.bottomView = bottomV;
   
    
    CGFloat btnW = 90;
    CGFloat btnH = 35;
    UIButton *sendGoodBtn = [[UIButton alloc]init];
    [sendGoodBtn setTitle:[UtilCheck language:@"local.confimUpdate"] forState:UIControlStateNormal];
    sendGoodBtn.titleLabel.font = CustomFont(14);
    sendGoodBtn.backgroundColor = [UIColor colorWithRed:21/255.0 green:63/255.0 blue:172/255.0 alpha:1.0];
    [self.bottomView addSubview:sendGoodBtn];
    sendGoodBtn.layer.cornerRadius = 3;
    [sendGoodBtn setFrame:CGRectMake(SCREEN_WIDTH -btnW-10, 10, btnW, btnH)];
    [sendGoodBtn addTarget:self action:@selector(confirmUp) forControlEvents:UIControlEventTouchUpInside];
    self.confirmBtn = sendGoodBtn;
}

#pragma -mark 请求可以修改的故障件
- (void)requestPlates {
    [[BDHUD sharedManager] showBDHUD:self.view title:@""];
    sleep(0.5);
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"orderCode"] = self.orderCode;
    params[@"parts"]= self.plate;
    
    [AFHttpTool postJsonDatawithUrl:@"/modifyParts" parameters:[params mutableCopy] success:^(id responseObject) {
        
        if(1==[[[responseObject objectForKey:@"head"] objectForKey:@"status"] intValue]){
            if([[responseObject objectForKey:@"data"] isKindOfClass:[NSArray class]]){
                //处理数据
                [self loadData:responseObject ];
                [self.tableView.header endRefreshing];
                [[BDHUD sharedManager] dismissHUD];
            }
            else
            {
                [self.tableView.header endRefreshing];
                [[BDHUD sharedManager] dismissHUD];
                
                
            }
        }
        else{
            //返回数据出问题 弹出服务器返回信息
            if([[[responseObject objectForKey:@"head"] allKeys] containsObject:@"info"]&&![UtilCheck DataIsNuLL:[[responseObject objectForKey:@"head"] objectForKey:@"info"]]){
                [[BDHUD sharedManager] showWords:[[responseObject objectForKey:@"head"] objectForKey:@"info"]];
            }
            else{
                [[BDHUD sharedManager] showWords:[THMangerTool language:@"HUD.nothing"]];
            }
        }
    } fail:^(id responseObject, NSError *error) {
        [self.tableView.header endRefreshing];
        [[BDHUD sharedManager] dismissHUD];
        if ([[error.userInfo objectForKey:@"NSLocalizedDescription"] isEqualToString:@"请求超时。"]) {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.time.out"]];
        }else {
            [[BDHUD sharedManager] showWords:[UtilCheck language:@"order.network.interruption"]];
        }
    }];
}

#pragma -mark 处理请求到的数据
-(void)loadData:(NSDictionary *)data
{
    NSArray *plates = [data objectForKey:@"data"];
    if (plates.count == 0) {
        [[BDHUD sharedManager]showWords:[UtilCheck language:@"HUD.noParts"]];
        self.nodata.hidden = NO;
       

    }
    else{
    NSMutableArray *muPlates = [NSMutableArray array];
        self.nodata.hidden= YES;
    
    for ( int i = 0 ; i<plates.count; i++) {
        
        SettleDeliverPlate *plate = [[SettleDeliverPlate alloc]init];
        [plate setValuesForKeysWithDictionary:plates[i]];
        
        int flag = -1;
        for (int j = 0 ; j < self.allRecommendArr.count ; j ++) {
            SettleDeliverPlate *model = self.allRecommendArr[j];
            
            if (![Toolobject IsEmptyString:plate.partsSN] && [plate.partsSN isEqualToString:model.partsSN] ) {
                flag = 0;
            }
        }
        
        if (flag < 0) {
            [muPlates addObject:plate];
        }
    }
    self.plateArr = muPlates;
    [self.tableView reloadData];
    }
}

#pragma mark 确认修改按钮点击
-(void)confirmUp
{
    if ([self.delegate respondsToSelector:@selector(getNewPlate:andTag:)]) {
        NSLog(@"tag---%ld",(long)self.tag);

        [self.delegate getNewPlate:self.confirmPlate andTag:self.plateTag];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

#pragma mark tableview的delegate and datasource
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    settleDeliveryUpCell  *cell =  [tableView dequeueReusableCellWithIdentifier:[NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row]];
    if (!cell) {
        cell = [[settleDeliveryUpCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[NSString stringWithFormat:@"cell%ld%ld",(long)indexPath.section,(long)indexPath.row]];
    }
    if (cell.alterBtn.tag != self.tag) {
        cell.alterBtn.selected = NO;
    }
    cell.delegate = self;
    cell.alterBtn.tag = indexPath.row;
    [cell setPlateModel:self.plateArr[indexPath.row]];
    __weak typeof(self) weakSelf = self;
    cell.selectBlock = ^(BOOL isSele,SettleDeliverPlate *plate){
        if (isSele == YES) {
            weakSelf.confirmBtn.userInteractionEnabled = YES;

            weakSelf.confirmPlate = plate;
            
            weakSelf.tag = indexPath.row;
            
        }
        if (isSele==NO) {
            weakSelf.confirmPlate = nil;
            weakSelf.confirmBtn.userInteractionEnabled = NO;

        }
    };
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    self.cellHeight = cell.cellHeight;
    return cell;
}

- (void)clearOtherBtn:(RZCustomButtom *)btn
{
    if (self.clickBtn) {
        self.clickBtn.selected = NO;
        self.clickBtn = btn;
        btn.selected = YES;
    }
    else
    {
        self.clickBtn = btn;
        btn.selected = YES;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    settleDeliveryUpCell *cell = (settleDeliveryUpCell *)[self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.cellHeight;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.plateArr.count;
}

@end
